 <?php
					
include 'conn.php';		

$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);

//
if($_GET['lx']=='GetListData'){

	$page = isset($data['page']) ? intval($data['page']) : 1;
	$rows = isset($data['size']) ? intval($data['size']) : 10;
	$offset = ($page-1)*$rows;
	
	$sort = isset($data['sort']) ? strval($data['sort']) : 'id';
	$order = isset($data['order']) ? strval($data['order']) : 'desc';
	
	$name = isset($data['Find']['name']) ? $data['Find']['name'] : '';
	$zd = isset($data['Find']['zd']) ? $data['Find']['zd'] : '';
	$nr = isset($data['Find']['nr']) ? $data['Find']['nr'] : '';

	$where = "name like '%$name%' and ".$zd." like '%$nr%'";
	$sql="select * from sectors where ".$where." order by $sort $order limit $offset,$rows";
	
	$rows = array();
	$rs =  mysqli_query($con,$sql);
	while($row = $rs->fetch_assoc()){
		array_push($rows,$row);
	} 

	$rs = mysqli_query($con,"select count(*) as num from sectors where ".$where." ");
	$row = $rs->fetch_assoc();
	$result["total"] = $row["num"];


	//1、--------权限 细化到菜单-----(表名和menu->name 一样！)---
	$sql_="SELECT * FROM menu WHERE name = '".$data['table']."'";
	$rows_ = array();
	$rs =  mysqli_query($con,$sql_);
	$rows_ = $rs->fetch_assoc();		

	$juserid=$data['userInfo']['juser_id'];		//角色
	$add_=contains_($rows_['added'],$juserid);		
	$del_=contains_($rows_['del'],$juserid);
	$modify_=contains_($rows_['modify'],$juserid);

	//2、-----如果【juser】表中的全局权限（add,del,modify）为真，则所有菜单权限为真
	//juser中变更权限后，需要重新登录获取新的juser权限！！！
	 if ($data['userInfo']['added'] == "1")
	 	$add_ = true;
		
	if ($data['userInfo']['del'] == "1")
		$del_ = true;
	
	if ($data['userInfo']['modify'] == "1")
		$modify_ = true;	


	$sj=date('Y-m-d-H:i:s');
	$user=$data['userInfo']['name'];
	$sql="INSERT INTO log(sj,user,action,ip)VALUES('".$sj."','".$user."','list','".getIP()."')";
	mysqli_query($con,$sql);


	$permissions['add']=$add_;
	$permissions['del']=$del_;
	$permissions['modify']=$modify_;

	$result["code"] = 200;
	$result["data"] = $rows;
	$result["message"] = "";
	$result["permissions"] = $permissions;
	$result["sql"] = $sql;
	echo json_encode($result);
	

}elseif($_GET['lx']=='sectorEditSave'){

	if($_GET['type']=='edit'){

		$sql="UPDATE sectors set ";
		foreach ($data as $k => $v) {		
			$sql=$sql. $k . "='" . $v . "',";
		}
		$sql=substr($sql,0,-1);		//去除最后一个字符 ','
		
		$sql=$sql." where id=".$data['id'];		
		mysqli_query($con,$sql);

		$result["message"] = "修改成功！";
	}else{
		$sql="INSERT INTO sectors(";	
	
		foreach ($data as $k => $v) {		
			$cols=$cols.$k.",";
			$value=$value."'".$v."',";
		}
		$cols=substr($cols,0,-1);	
		$value=substr($value,0,-1);

		$sql=$sql.$cols.")VALUES(".$value.")";
		mysqli_query($con,$sql);

		$result["message"] = "添加成功！";
	}

	$result["code"] = 200;
	$result["sql"] = $sql;
	echo json_encode($result);
	
}elseif($_GET['lx']=='uploadExel'){	

	if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
		// 保存上传的文件
		$targetDir = '../tmp/';
		$targetFile = $targetDir . basename($_FILES['file']['name']);
		if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
			// 文件保存成功

			require '../vendor/autoload.php';

			$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
			$reader->setReadDataOnly(TRUE);
			$spreadsheet = $reader->load( $targetFile); //载入excel表格
			
			$worksheet = $spreadsheet->getActiveSheet();
			$highestRow = $worksheet->getHighestRow(); // 总行数
			$highestColumn = $worksheet->getHighestColumn(); // 总列数
			$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5
					
			$sql = "INSERT INTO `sectors_copy1` (`name`, `sex`, `mz`, `brithday`) VALUES ";

			$num=(int)$_POST["num"];
			for ($row = $num; $row <= $highestRow; ++$row) {
				$name = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); //姓名 2列
				$chinese = $worksheet->getCellByColumnAndRow(3, $row)->getValue(); 
				$maths = $worksheet->getCellByColumnAndRow(4, $row)->getValue(); 
				$english = $worksheet->getCellByColumnAndRow(5, $row)->getValue(); 
			
				$sql .= "('$name','$chinese','$maths','$english'),";
			}
			$sql = rtrim($sql, ","); //去掉最后一个,号
			try {
				$con->query($sql);

				$num=$highestRow-$num+1;
				$result["code"] = 200;
				$result["file"] = $targetFile;
				$result["message"] = "导入成功，共计【". $num ."】条";
			} catch (Exception $e) {
				
				$result["code"] = 200;
				$result["message"] = $e->getMessage();
			}

			
		} else {
			// 文件保存失败
			$result["code"] = 201;
			$result["message"] = "Failed to save file.";
		}
	} else {
		// 文件上传失败
		$result["code"] = 201;
		$result["message"] = "File upload failed.";
		
	}

	echo json_encode($result);

}else{
	$result["code"] = 201;
	$result["message"] = "else";
	echo json_encode($result);
	
}
  
?>